package com.thrd.taiexhibition.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.generic.RoundingParams;
import com.thrd.basicframework.ui.recyclerview.SpacesItemDecoration;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.bean.BeanCityInvest;
import com.thrd.taiexhibition.bean.BeanCityInvestArticle;
import com.thrd.taiexhibition.bean.BeanCityInvestDay;
import com.thrd.taiexhibition.fresco.FrescoImage;
import com.thrd.taiexhibition.fresco.FrescoUtils;

import java.util.List;

/**
 * 投资环境右边
 * @author: ${唐杰}
 * @date: 2021/3/17
 */
public class InvestmentEnvironmentRightAdapter extends RecyclerView.Adapter {
    public List<BeanCityInvestDay> listString;
    private Context context;
    private final LayoutInflater mLayoutInflater;
    public InvestmentEnvironmentRightAdapter(Context context,List<BeanCityInvestDay> listString){
        this.context=context;
        this.listString=listString;
        mLayoutInflater = LayoutInflater.from(context);
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==0){
            View view=mLayoutInflater.inflate(R.layout.item_invest_right,parent,false);
            return new ImageHolder(view);
        }else if(viewType==1){
            View view=mLayoutInflater.inflate(R.layout.item_invest_policy,parent,false);
            return new Investmentitem(view);
        }else{
            View view=mLayoutInflater.inflate(R.layout.item_invest_null,parent,false);
            return new NullHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ImageHolder){
            ImageHolder imageHolder=    (ImageHolder) holder;
            final BeanCityInvestDay  beanCityInvestDay  =listString.get(position);
//            imageHolder.mFrescoImage.setImageUrl(beanCityInvestDay.getImageBg());
            FrescoUtils.getInstance().setPrefixProgressLoad(context,imageHolder.mFrescoImage,beanCityInvestDay.getImageBg(), RoundingParams.fromCornersRadius(0));
            FrescoUtils.getInstance().setPrefixProgressLoad(context,imageHolder.mFrescoImageCode,beanCityInvestDay.getImageQrCode(), RoundingParams.fromCornersRadius(5f));
            imageHolder.mFrescoImageCode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onInvestmentEnvironmentRight!=null){
                        onInvestmentEnvironmentRight.onQrCodeListener(v,beanCityInvestDay.getImageQrCode());
                    }
                }
            });

        }else if(holder instanceof Investmentitem){
            final BeanCityInvestDay mBeanCityInvestDay  =listString.get(position);
            Investmentitem imageHolder=    (Investmentitem) holder;


            if(mBeanCityInvestDay!=null){
                List<BeanCityInvest> mBeanCityInvests=  mBeanCityInvestDay.getBeanCityInvests();
                InvestmentHorizontalAdapter mAdapter;
                if(imageHolder.mRecyclerView.getAdapter()==null){
                    mAdapter=new InvestmentHorizontalAdapter(context,mBeanCityInvests);
                    imageHolder.mRecyclerView.setAdapter(mAdapter);
                }else{
                    mAdapter = (InvestmentHorizontalAdapter) imageHolder.mRecyclerView.getAdapter();
                    mAdapter.refresh(mBeanCityInvests);
                }


                mAdapter.setOnItemInvestmentItem(new InvestmentHorizontalAdapter.OnItemInvestmentItem() {
                    @Override
                    public void onItemInvestmentItem(BeanCityInvestArticle mBeanCityInvestArticle) {
                        if(onClickCityInvestArticle!=null){
                            onClickCityInvestArticle.OnClickCityInvestArticle(mBeanCityInvestArticle);
                        }
                    }
                });

//                if(mBeanCityInvests!=null&&mBeanCityInvests.size()>0){
//                    imageHolder.mInvestmentItem1.setBeanCityIndustry(mBeanCityInvests.get(0));
//                    imageHolder.mInvestmentItem1.setOnClickItemListener(new InvestmentItem.OnClickItemListener() {
//                        @Override
//                        public void onClickItemListener(BeanCityInvestArticle t) {
//                            if(onClickCityInvestArticle!=null){
//                                onClickCityInvestArticle.OnClickCityInvestArticle(t);
//                            }
//                        }
//                    });
//                }
//                if(mBeanCityInvests!=null&&mBeanCityInvests.size()>1){
//                    imageHolder.mInvestmentItem2.setBeanCityIndustry(mBeanCityInvests.get(1));
//                    imageHolder.mInvestmentItem2.setOnClickItemListener(new InvestmentItem.OnClickItemListener() {
//                        @Override
//                        public void onClickItemListener(BeanCityInvestArticle t) {
//                            if(onClickCityInvestArticle!=null){
//                                onClickCityInvestArticle.OnClickCityInvestArticle(t);
//                            }
//                        }
//                    });
//                }
//                if(mBeanCityInvests!=null&&mBeanCityInvests.size()>2){
//                    imageHolder.mInvestmentItem3.setBeanCityIndustry(mBeanCityInvests.get(2));
//                    imageHolder.mInvestmentItem3.setOnClickItemListener(new InvestmentItem.OnClickItemListener() {
//                        @Override
//                        public void onClickItemListener(BeanCityInvestArticle t) {
//                            if(onClickCityInvestArticle!=null){
//                                onClickCityInvestArticle.OnClickCityInvestArticle(t);
//                            }
//                        }
//                    });
//                }
//                if(mBeanCityInvests!=null&&mBeanCityInvests.size()>3){
//                    imageHolder.mInvestmentItem4.setBeanCityIndustry(mBeanCityInvests.get(3));
//                    imageHolder.mInvestmentItem4.setOnClickItemListener(new InvestmentItem.OnClickItemListener() {
//                        @Override
//                        public void onClickItemListener(BeanCityInvestArticle t) {
//                            if(onClickCityInvestArticle!=null){
//                                onClickCityInvestArticle.OnClickCityInvestArticle(t);
//                            }
//                        }
//                    });
//                }

                FrescoUtils.getInstance().setPrefixProgressLoad(context,imageHolder.mFrescoImage,mBeanCityInvestDay.getImageBg(), RoundingParams.fromCornersRadius(5f));

                imageHolder.mFrescoImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(onClickCityInvestArticle!=null){
                            onClickCityInvestArticle.OnClickExhibition(mBeanCityInvestDay.getId());
                        }
                    }
                });
            }



        }
    }

    @Override
    public int getItemCount() {
        return listString==null?0:listString.size();
    }

    @Override
    public int getItemViewType(int position) {
        return listString.get(position).getInvestType();
    }

    /**
     * 图片
     */
    private  class ImageHolder extends RecyclerView.ViewHolder {

        private final FrescoImage mFrescoImage;
        private final FrescoImage mFrescoImageCode;

        public ImageHolder(@NonNull View itemView) {
            super(itemView);
            mFrescoImage = itemView.findViewById(R.id.fi_image);
            mFrescoImageCode=itemView.findViewById(R.id.fi_image_code);
        }
    }


    /**
     * 图片
     */
    private  class NullHolder extends RecyclerView.ViewHolder {



        public NullHolder(@NonNull View itemView) {
            super(itemView);

        }
    }


    private class Investmentitem  extends RecyclerView.ViewHolder {
        private TextView mTitle;
        //        private InvestmentItem mInvestmentItem1;
//        private InvestmentItem mInvestmentItem2;
//        private InvestmentItem mInvestmentItem3;
//        private InvestmentItem mInvestmentItem4;
        private FrescoImage mFrescoImage;
        private RecyclerView mRecyclerView;

        public Investmentitem(@NonNull View itemView) {
            super(itemView);
            mTitle = itemView.findViewById(R.id.tv_title);
            mRecyclerView=itemView.findViewById(R.id.rv_recyclerview);
            LinearLayoutManager mLayoutManager=new LinearLayoutManager(itemView.getContext());
            mLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.addItemDecoration(new SpacesItemDecoration(0,0,itemView.getContext().getResources().getDimensionPixelSize(R.dimen.qb_px_10),0));
            mFrescoImage= itemView.findViewById(R.id.fi_image);

        }

    }




    public void setData(List<BeanCityInvestDay> listString){
        this.listString=listString;
        notifyDataSetChanged();

    }


    private OnInvestmentEnvironmentRight onInvestmentEnvironmentRight;

    public OnInvestmentEnvironmentRight getOnInvestmentEnvironmentRight() {
        return onInvestmentEnvironmentRight;
    }

    public void setOnInvestmentEnvironmentRight(OnInvestmentEnvironmentRight onInvestmentEnvironmentRight) {
        this.onInvestmentEnvironmentRight = onInvestmentEnvironmentRight;
    }

    public   interface OnInvestmentEnvironmentRight{
        void onQrCodeListener(View v,String path);
    }

    private OnClickCityInvestArticle onClickCityInvestArticle;

    public OnClickCityInvestArticle getOnClickCityInvestArticle() {
        return onClickCityInvestArticle;
    }

    public void setOnClickCityInvestArticle(OnClickCityInvestArticle onClickCityInvestArticle) {
        this.onClickCityInvestArticle = onClickCityInvestArticle;
    }

    public interface OnClickCityInvestArticle{
        void OnClickCityInvestArticle(BeanCityInvestArticle t);
        void OnClickExhibition(long exhibition);
    }
}
